package com.webvihar.access2db.activities;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.webvihar.access2db.R;
import com.webvihar.access2db.email.*;
import com.webvihar.common.utils.Logger;

public class SecurityCheckActivity extends BaseActivity implements OnClickListener, TextWatcher{

	private TextView titleTxt;
	private ImageButton addButton;

	private EditText sc1Txt;
	private EditText sc2Txt;
	private EditText sc3Txt;
	private EditText sc4Txt;

	private TextView forgotScTxt;

	private String emailIdFromSplash;
	private String securityCode;

	private static final String EMAIL_ID = "access2db.testing@gmail.com";
	private static final String EMAIL_PASSWORD = "1@ABCDEFG";
	private static final String EMAIL_SUBJECT = "Security Code for Access2Db";
	
	/** Called when the activity is first created. **/
	@Override 
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left); 
		setContentView(R.layout.security_check);
		initIntentParameters();
		setTitleBar();
		initUI();
		setListeners();
	}

	private void initIntentParameters() {
		Bundle bundle = getIntent().getExtras();
		emailIdFromSplash = bundle.getString(KEY_EMAIL_ID);
		securityCode = bundle.getString(KEY_SECURITY_CODE);
	}

	private void setTitleBar() {
		titleTxt = (TextView) findViewById(R.id.title_txt);
		titleTxt.setText(getString(R.string.access2db_txt));
		addButton = (ImageButton) findViewById(R.id.title_action_image_btn);
		addButton.setVisibility(View.GONE);
	}


	private void initUI() {
		sc1Txt = (EditText)findViewById(R.id.security_check_code1_et);
		sc2Txt = (EditText)findViewById(R.id.security_check_code2_et);
		sc3Txt = (EditText)findViewById(R.id.security_check_code3_et);
		sc4Txt = (EditText)findViewById(R.id.security_check_code4_et);
		forgotScTxt = (TextView) findViewById(R.id.security_check_forgot_security_code_tv);
	}

	private void setListeners() {
		sc1Txt.addTextChangedListener(this);
		sc2Txt.addTextChangedListener(this);
		sc3Txt.addTextChangedListener(this);
		sc4Txt.addTextChangedListener(this);
		forgotScTxt.setOnClickListener(this);
	}

	private void updateFocus(EditText prev, EditText cur, EditText next) {
		if(cur.getText().toString().length() == 1) {
			if(next != null)
				next.requestFocus();
		} else {
			if(prev != null)
				prev.requestFocus();
		}
	}

	private boolean isSecurityCodeValid(String enteredSecurityCode) {
		return (enteredSecurityCode.equalsIgnoreCase(securityCode)) ? true : false;
	}

	@Override
	public void onClick(View v) {
		if(v == forgotScTxt) {
			// TODO: yet to use other email authentication to send mail
			try {   
				GMailSender sender = new GMailSender(EMAIL_ID, EMAIL_PASSWORD);
				sender.sendMail(EMAIL_SUBJECT,   
								String.format("Hi," +
										"\n\tYou security code for Access2Db is \"%s\"." +
										"\n\nThanks&Regards," +
										"\nAccess2Db", 
										securityCode),   
										EMAIL_ID,   
								emailIdFromSplash);
				showToast(getString(R.string.toast_mail_sent));   
			} catch (Exception e) {
				showToast(getString(R.string.toast_mail_sending_failed));
				Logger.logError("SendMail", e.getMessage(), e);   
			} 

		}
	}

	@Override
	public void afterTextChanged(Editable s) {
		String enteredSecurityCode = String.format("%s%s%s%s", sc1Txt.getText().toString(), sc2Txt.getText().toString(), sc3Txt.getText().toString(), sc4Txt.getText().toString());
		if(enteredSecurityCode.length() == 4 && isSecurityCodeValid(enteredSecurityCode)) {
			Intent intent =new Intent(SecurityCheckActivity.this, SessionsActivity.class);
			startActivity(intent);
		} else { 
			if(enteredSecurityCode.length() == 4) {
				showToast(getString(R.string.toast_invalid_security_code));
				sc1Txt.setText("");
				sc2Txt.setText("");
				sc3Txt.setText("");
				sc4Txt.setText("");
				sc1Txt.requestFocus();
			} else if(sc1Txt.hasFocus()) {
				updateFocus(null, sc1Txt, sc2Txt);
			} else if(sc2Txt.hasFocus()) {
				updateFocus(sc1Txt, sc2Txt, sc3Txt);
			} else if(sc3Txt.hasFocus()) {
				updateFocus(sc2Txt, sc3Txt, sc4Txt);
			} else if(sc4Txt.hasFocus()) {
				updateFocus(sc3Txt, sc4Txt, null);
			}
		}
	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {		
	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
	}
}
